<?php
/**
 * Created by PhpStorm.
 * User: clcw908
 * Date: 2016/12/27
 * Time: 11:41
 */
date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER['REQUEST_URI'])) {
	die('error');
}

if (!defined('API_ROOT_PATH')) {
	define('API_ROOT_PATH', dirname(__FILE__));
}

require(API_ROOT_PATH . '/../inc/common.php');
require(API_ROOT_PATH . '/../inc/Config.php');
require(API_ROOT_PATH . '/../inc/MysqliDb.php');

//初始化数据库链接
/**
 * @return MysqliDb
 */
function init_db()
{
	$db = new MysqliDb ([
		'host' => MYSQLDB,
		'username' => MYSQLDB_USER,
		'password' => MYSQLDB_PWD,
		'db' => MYSQLDB_DATABASE,
		'port' => 3306,
		'prefix' => 'au_',
		'charset' => 'utf8'
	]);
	return $db;
}
$db = init_db();

$src_id = 0;
$lastId = 0;

while (true){

	$list = $db->where('status',2)->where('src_id',$lastId,'>')->get('self_receive_car',2000);
	if(empty($list)){
		logUtil('处理完成');die();
	}
	foreach ($list as $key=>$val){

		$info = judgeRefundInfoExist($val['src_id'],2);
		if($info){ //如果退车信息已存在，就不在执行
			$lastId = $val['src_id'];
			logUtil('自收车辆编号:'.$val['src_id'].'退车信息已经存在');
			continue;
		}

		$db->startTransaction();
		$data = [
			'order_id' => $val['order_id'],
			'car_id' => $val['car_id'],
			'sid' => $val['sid'],
			'refer_id' => $val['src_id'],
			'type' => 2, //2 自收车辆
			'status' => 3, // 3 已退车
			'compensate' => $val['compensate'],
			'back_reason' => empty($val['back_reason']) ? '':$val['back_reason'],
			'collection_id' => empty($val['collection_id']) ? '' : $val['collection_id'],
			'collection_id_car' => empty($val['collection_id_car']) ? '': $val['collection_id_car'],
			'payment_id' => empty($val['payment_id']) ? '':$val['payment_id'],
			'remark_amount_of_car' => empty($val['remark_amount_of_car']) ? '':$val['remark_amount_of_car'],
			'remark_payment' => empty($val['remark_payment']) ? '' : $val['remark_payment'],// finish_emp_id,finish_emp_name
			'create_time' => $val['createtime'],
			'finish_time' => $val['dealtime'],
			'finish_emp_id'=> $val['emp_id'],
			'remark'=> empty($val['remark']) ? '' : $val['remark'],
		];
		$result = addRefund($data);
		if(!$result){
			$db->rollback();
			logUtil('自收车辆编号:'.$val['src_id'].'添加退车信息失败');
			die();
		}

		$result = editeCarsByCarId($val['car_id']);
		if(!$result){
			$db->rollback();
			logUtil('自收车辆编号:'.$val['src_id'].'更新车辆报告信息失败');
			die();
		}

		$db->commit();
		$lastId = $val['src_id'];
		logUtil('自收车辆编号:'.$val['src_id'].'处理退车信息成功');
	}
}


/**
 * 添加退车信息
 *
 * @param $data 退车数据
 * @return bool
 */
function addRefund($data){
	global $db;
	if($data['finish_emp_id']){
		$employData = $db->where('emp_id',$data['finish_emp_id'])->getOne('employee');
		$data['finish_emp_name'] = $employData['real_name'];
	}

	$result = $db->insert('refund_car',$data);
	//echo $db->getLastQuery();
	return $result;
}


/**
 * 更改车辆信息，变成已退车
 *
 * @param $carId
 * @return bool
 */
function editeCarsByCarId($carId){
	global $db;
	$data = [
		'car_status'=> 11, //已退车
	];
	return $db->where('car_id',$carId)->update('cars',$data);
}


/**
 * 判断退车信息是否存在
 *
 * @param $referId 来源编号
 * @param $type 车辆类型
 * @return mixed
 */
function judgeRefundInfoExist($referId, $type){
	global $db;
	$result = $db->where('refer_id',$referId)->where('type',$type)->getOne('refund_car');
	//echo $db->getLastQuery();
	return $result;
}